/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package erp.financeiro.servlets;

import erp.financeiro.Saldo;
import erp.financeiro.dao.SaldoDAO;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.beans.XMLDecoder;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 *
 * @author Robert
 */
public abstract class FinanceiroServlet extends HttpServlet {

    private HttpSession session;

    private Boolean authorized;
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        this.setSession(request.getSession());

        if(this.getSession().getAttribute("usuario") == null){
            if(request.getParameter("token") != null){
                // acessa o serviço:  http://www.sp-administracao.com.br/api/autorizado?token=
                StringBuilder sb = new StringBuilder();
                sb.append("http://www.sp-administracao.com.br/api/autorizado?token=").append(request.getParameter("token"));

                //acha usuario
                Object usuario = null;
                URL usuarioTest = new URL(sb.toString());
                try {
                    InputStream inputStream = usuarioTest.openStream();

                    SAXBuilder saxb = new SAXBuilder();

                    Document d = saxb.build(inputStream);
                    Element root = d.getRootElement();
                    
                    this.getSession().setAttribute("usuario", root);
                    this.setAuthorized(Boolean.TRUE);

                    inputStream.close();
                }catch(IOException eIO){
                    response.sendRedirect("http://www.sp-administracao.com.br/login?returnto=http://localhost:8080/erpfinanceiro/FluxoCaixaServlet");
                    this.setAuthorized(Boolean.FALSE);
                }catch(JDOMException e){
                    response.sendRedirect("http://www.sp-administracao.com.br/login?returnto=http://localhost:8080/erpfinanceiro/FluxoCaixaServlet");
                    this.setAuthorized(Boolean.FALSE);
                }
                if(usuario != null){
                    this.getSession().setAttribute("usuario", usuario);
                    this.setAuthorized(Boolean.TRUE);
                }
            } else {
                response.sendRedirect("http://www.sp-administracao.com.br/login?returnto=http://localhost:8080/erpfinanceiro/FluxoCaixaServlet");
                this.setAuthorized(Boolean.FALSE);
            }
        } else {
            this.setAuthorized(Boolean.TRUE);
        }
    }

    public Boolean getAuthorized() {
        return authorized;
    }

    public void setAuthorized(Boolean authorized) {
        this.authorized = authorized;
    }
    
    public HttpSession getSession() {
        return this.session;
    }

    public void setSession(HttpSession session) {
        this.session = session;
    }




    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
